package my2Cents;

import java.io.Serializable;

import net.phys2d.math.Vector2f;
import net.phys2d.raw.Body;
import net.phys2d.raw.StaticBody;
import net.phys2d.raw.World;
import net.phys2d.raw.shapes.PhysShape;

public class BodyState implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -3273270707786967826L;
	
	private Body body;
	private Vector2f pos;
	private Vector2f force;
	private Vector2f velocity;
	private float rotation;
	private PhysShape shape;
	private float mass;
	private float torque;
	private float angVel;
	
	public BodyState(Body body) {
		this.body = body;
		this.pos = (Vector2f) new Vector2f(body.getPosition());
		this.force = (Vector2f) new Vector2f(body.getForce());
		this.velocity = (Vector2f) new Vector2f(body.getVelocity());
		this.rotation = body.getRotation();
		this.shape = body.getShape();
		this.mass = body.getMass();
		this.torque = body.getTorque();
		this.angVel = body.getAngularVelocity();
	}
	
	public Body getBody() {return body;}
	public PhysShape getShape() {return shape;}
	public Vector2f getPos() {return pos;}
	public float getRotation() {return rotation;}
	
	public Body getNewBody() {
		if(body instanceof SpecialBody) {
			SpecialBody prev = (SpecialBody) body;
			Body b = prev.duplicate();
			resolveBody(b);
			return b;
		} else if(body.isStatic()) {
			Body b = new StaticBody(shape);
			resolveBody(b);
			return b;
		} else {
			Body b = new Body(shape,mass);
			resolveBody(b);
			return b;			
		}
	}
	
	public void resolveBody(Body b) {
		b.setPosition(pos.x, pos.y);
		b.setRotation(rotation);
		if(!body.isStatic()) {
			b.setForce(force.x, force.y);
			b.setTorque(torque);
			b.setAngularVelocity(angVel);
			b.setVelocity(velocity);
		}
		b.setDrawColor(getBody().getDrawColor());
		b.setRemoveOther(getBody().isRemoverField());
		b.setMotorSpeed(getBody().getMotorSpeed());
	}
	
}
